<?php
  session_cache_limiter('private,must-revalide');
   /**
   * 处理上传页面
   */  
  include_once("./config.php");
  $tpl = new PhplibTpl($conf["admin_tpl_path"]);
  
  if ($_POST['type'])
  {	
	$name = work_str($_POST["name"], 1);		
	$tel = work_str($_POST["tel"], 1);		
	$addr = work_str($_POST["addr"], 1);		
	$contact = work_str($_POST["contact"], 1);		
	if($name==''||$tel==''||$addr==''||$contact=='')
	{	
		 MyNotice("請輸入完整的信息", 1, "news.php");	
		 exit(0);
	}
	if($_POST[id]!='')
	{
		$id = $_POST[id];		
		$sel = "select * from $TBL_STORE where name ='$name' and id!=$id";
		$result = mysql_query($sel);
		if(mysql_fetch_array($result))
		{
			 MyNotice("該分店名稱已經存在", 1, "news.php");
			 exit(0);
		}
		$update = "update $TBL_STORE set name ='$name',addr='$addr',tel='$tel',contact='$contact' where id=$id";
		$res = mysql_query($update) or die(mysql_error());
		if($res)
		{
			 MyNotice("分店編輯成功", 1, "news.php");
			 exit(0);
		}	
	
	}else{
		$sel = "select * from $TBL_STORE where name ='$name'";
		$result = mysql_query($sel);
		if(mysql_fetch_array($result))
		{
			 MyNotice("該分店名稱已經存在", 1, "news.php");
			 exit(0);
		}
		$query = "INSERT INTO $TBL_STORE (id,name,addr,tel,contact) values (NULL,'$name','$addr','$tel','$contact')";	
		$res = mysql_query($query) or die(mysql_error());
		if($res)
		{
			 MyNotice("分店添加成功", 1, "news.php");
			 exit(0);
		}
	}	
		
  } 
  include_once("list_cate.php");
  $time1=date('Y-m-d H:i:s');
  $tpl -> set_file(array("main" => "fd.htm"));
  $tpl -> set_block("main", "NEWS_LIST", "news_list");

  $query = "SELECT * FROM $TBL_STORE order by id asc";
  $result = mysql_query( $query ) or die(mysql_error());
  $count = 0;
  while($rows = mysql_fetch_array( $result))
  { 
	++$count;
	if($rows['id']==1)
	{
		$show1 =$show1.'分店'.$count." - ".$rows['name']."&nbsp;&nbsp;".$rows['addr']."&nbsp;&nbsp;".$rows['contact']."&nbsp;&nbsp;".$rows['tel']." - <a href='?mode=fd&id=$rows[id]'>編輯</a><br>"; 
	}else{
		$show1 =$show1.'分店'.$count." - ".$rows['name']."&nbsp;&nbsp;".$rows['addr']."&nbsp;&nbsp;".$rows['contact']."&nbsp;&nbsp;".$rows['tel']." - <a href='?mode=fd&id=$rows[id]'>編輯</a>  -  <a href='delete_fd.php?id=$rows[id]'>刪除</a><br>"; 	  
	}
	  
  }
  if($_GET['id'])
  {
	  $id = $_GET['id'];
	  $query2 = "SELECT * FROM $TBL_STORE where id=$id";
	  $result2 = mysql_query( $query2 ) or die(mysql_error());	
	  $rows2 = mysql_fetch_array( $result2);
	  $botton ='編輯';
  }else{
	  $botton ='增加';
  }


  $tpl -> set_var(array(                        
						 "show1" => $show1,
						 "id" => $rows2['id'],			
						 "name" => $rows2['name'],			
						 "addr" => $rows2['addr'],			
						 "tel" => $rows2['tel'],			
						 "contact" => $rows2['contact'],	
						 "botton" => $botton,
                       ));
					   
  $tpl->set_var($conf);
  $tpl -> parse("Output", "main");
  $tpl -> p("Output");
  exit(0);
?>